package com.example.uploadfile.controller;

import com.example.uploadfile.pojo.UploadFileBO;
import com.example.uploadfile.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.UUID;

@RestController
@RequestMapping("upload")
public class UploadFileController {

    @Autowired
    private UploadFileService uploadFileService;

    @PostMapping(value = "/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Boolean file(HttpServletRequest request) {
        UploadFileBO uploadFileBO = new UploadFileBO();
        uploadFileBO.setTaskId(UUID.randomUUID().toString());
        uploadFileBO.setBatchId(request.getParameter("batchId"));
        MultipartHttpServletRequest mulReq=(MultipartHttpServletRequest)request;
        uploadFileBO.setMultipartFile(mulReq.getFile("multipartFile"));
        return uploadFileService.uploadFile(uploadFileBO);
    }

    @PostMapping(value = "/file2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Boolean file(@RequestParam(value = "multipartFile") MultipartFile multipartFile,
                        @RequestParam(value = "batchId") String batchId) {
        UploadFileBO uploadFileBO = new UploadFileBO();
        uploadFileBO.setTaskId(UUID.randomUUID().toString());
        uploadFileBO.setBatchId(batchId);
        uploadFileBO.setTaskType(1);
        uploadFileBO.setMultipartFile(multipartFile);
        return uploadFileService.uploadFile(uploadFileBO);
    }
}
